import urllib2,urllib,mechanize


def saveAndApply(br, proposal_id, margin, fin_product_id, down, ann_increase, install_this_year):
    data = {}
    data["proposal_id"] = proposal_id
    data["financing_product_id"] = fin_product_id
    if down!="":
        data["down_payment"] = down
    if ann_increase!="":
        data["annual_increase"] = ann_increase
    if proposal_id=="31":
        data["billing_surcharge"] = "1"
    data["id"] = ""
    data["install_this_year"] = install_this_year
    data["is_primary"] = "1"
    data["margin"] = margin
    data = urllib.urlencode(data)
    url = "https://tools.cleanpowerfinance.com/quoting/financingcomparison/validate/on_success_action/save" #excluded_elements/writeoff_monthly_payment"
    response = br.open(url,data)
    return br


#br = mechanize.Browser(factory=mechanize.RobustFactory())
#br.set_handle_robots(False)
#br.open("https://tools.cleanpowerfinance.com/user/login")
#br.select_form("loginform")
#br["username"]="lwooley@completesolar.com"
#br["password"]="solar123solar"
#br.submit()
#print saveAndApply(br,"174851", "20", "32", "", "", "1").response().read()
